package utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ReadNWriteAFile {
	public static void main(String[] args){
		//readFile("CAD1.txt");
		readFile3("C:\\Documents and Settings\\829929\\cadDump\\june17\\CAD1_copy.txt");

		/*try {
			writeFile(readFile("test.txt"), "test2.txt");
		} catch (IOException e) {
			e.printStackTrace();
		} */
	}
	
	//read a file converting to byteArray of String
	public static byte[] readFile(String fileName){
		byte[] bytes = null;
		try {
			File file = new File(fileName);
			InputStream in = new FileInputStream(file);
			bytes = new byte[(int) file.length()];
			in.read(bytes);
			//-----------------testing---------------------
			/*String str = new String(bytes, 0, bytes.length).trim();
			//StringBuffer sb = new StringBuffer(bytes).trim();
			System.out.println(str);
			for(int i=0; i < bytes.length; i++)
				System.out.println(bytes[i]);*/
			//------------testing------------------------
			in.close();
			//return bytes;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bytes;
		
	}
	
	//read a file converting to byteArray of StringBuffer adding 2 bytes
	public static byte[] readFile2(String fileName){
		//store into string first
		String str = new String(readFile(fileName));
		//then convert to StringBuffer
		StringBuffer sb = new StringBuffer(str);
		//finally convert to byteArray adding 2 bytes
		byte[] bytes = stringBufferToByte(sb);
		/*System.out.println("readFile2");
		for(int i=0; i < bytes.length; i++)
			System.out.println(bytes[i]);*/
		return bytes;
		
	}
	
	//read a file converting to byteArray of StringBuffer not adding 2 bytes
	public static byte[] readFile3(String fileName){
		//store into string first
		String str = new String(readFile(fileName));
		//then convert to StringBuffer
		StringBuffer sb = new StringBuffer(str);
		//finally convert to byteArray
		byte[] bytes = stringBufferToByte2(sb);
		/*System.out.println("readFile3");
		for(int i=0; i < bytes.length; i++)
			System.out.println(bytes[i]);
		System.out.println("last byte: " + bytes[bytes.length-1]);*/
		return bytes;
		
	}
	
	//write byteArray into a file, regardless of type String or StringBuffer
	public static void writeFile(byte[] data, String fileName) throws IOException{
		try{
			OutputStream out = new FileOutputStream(fileName);
			out.write(data);
			out.close();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	//convert a StringBuffer to a byteArray adding 2 bytes
	public static byte[] stringBufferToByte(StringBuffer ediMsg){
        int mLen = ediMsg.length();
        byte bytes[] = new byte[mLen + 2];
        bytes[0] = (byte)(mLen / 256);
        bytes[1] = (byte)(mLen % 256);
        for(int i = 2; i < mLen + 2; i++){
            bytes[i] = (byte)ediMsg.charAt(i - 2);
        }
        return bytes;
	 }
	
	//convert a StringBuffer to a byteArray, NOT adding 2 bytes in front
	public static byte[] stringBufferToByte2(StringBuffer ediMsg){
        int mLen = ediMsg.length();
        byte bytes[] = new byte[mLen];
        for(int i = 0; i < mLen; i++){
            bytes[i] = (byte)ediMsg.charAt(i);
        }
        return bytes;
	 }
}